#include "qs_config.h"
#include "qs_log.h"
#include "qs_sched.h"
#include "qs_pwm.h"
#include "qs_misc.h"
#include "qs_all_keys.h"
#include "qs_rf_protocol.h"
#include "qs_ble_protocol.h"
#include "qs_settings.h"
#include "AIP650.h"
#include "qs_aip650.h"
#include "qs_action.h"
#include "qs_flash.h"
static uint32_t m_rf_keys;
static uint16_t m_rf_keys_timeout_timer = 0;
static qs_key_on_key_evt_handle_s m_on_key_evt = NULL;
static qs_key_on_rx_data_handle_s m_on_rf_rx_data = NULL;
static qs_key_on_ble_rx_data_handle_s m_on_ble_rx_data = NULL;
//static const uint16_t keys_tab[] = {
//    QS_KEYS_FAN_UP,         QS_KEYS_FAN_DOWN,              QS_KEYS_HEAT_UP,    QS_KEYS_HEAT_DOWN,
//    QS_KEYS_POWER,          QS_KEYS_TIMER,       QS_KEYS_AUTO
//};
//static const uint8_t keys_press_tab[] = {
//    QS_KEY_EVT_PRESS_FAN_UP,         QS_KEY_EVT_PRESS_FAN_DOWN,      QS_KEY_EVT_PRESS_HEAT_UP,    QS_KEY_EVT_PRESS_HEAT_DOWN,
//    QS_KEY_EVT_PRESS_POWER,          QS_KEY_EVT_PRESS_TIMER,    QS_KEY_EVT_PRESS_AUTO
//};

static const uint16_t keys_tab[] = {
    QS_KEYS_HEAT_UP,    QS_KEYS_HEAT_DOWN,     QS_KEYS_POWER,           QS_KEYS_AUTO
};
static const uint8_t keys_press_tab[] = {
     QS_KEY_EVT_PRESS_HEAT_UP,    QS_KEY_EVT_PRESS_HEAT_DOWN,  QS_KEY_EVT_PRESS_POWER,  QS_KEY_EVT_PRESS_AUTO
};







static void on_keys_20ms_handle(void)
{
//    uint32_t keys = m_rf_keys | qs_ble_keys_result_get() | (qs_aip650_key_get() ? QS_KEYS_POWER:QS_KEYS_RELEASE );
    uint32_t keys = m_rf_keys | qs_ble_keys_result_get() | qs_aip650_key_get() ;
    
    
    
    static uint32_t last_keys = QS_KEYS_RELEASE;
    static uint16_t keys_repeat = 0;
    

    if(keys == last_keys ) {
        if(keys_repeat < UINT16_MAX) keys_repeat ++;
        
        if(keys == QS_KEYS_POWER && keys_repeat == 120 && m_on_key_evt) {
                m_on_key_evt(QS_KEY_EVT_HOLD_POWER_3S);
        }
    } else if(last_keys == QS_KEYS_RELEASE && keys != QS_KEYS_RELEASE ) {
//        QS_LOG_INFO("keys %x press\r\n", keys);
        qs_misc_beep_once();
        keys_repeat = 0;
    } else if (keys == QS_KEYS_RELEASE && last_keys != QS_KEYS_RELEASE && (keys_repeat<120)) {
        keys_repeat = 0;
        for(int idx = 0;idx < sizeof(keys_tab)/sizeof(uint16_t); idx ++) {
            if(last_keys == keys_tab[idx] && m_on_key_evt) {
                m_on_key_evt(keys_press_tab[idx]);
            }
        }
        
//        if(m_on_key_evt) {
//            m_on_key_evt(QS_KEY_EVT_RELEASE);
//        }
    }
    if(m_rf_keys_timeout_timer > 0){
      m_rf_keys_timeout_timer--;
      if(m_rf_keys_timeout_timer == 0){
        m_rf_keys = 0;
      }
    }
    last_keys = keys;
    
}




static void on_rf_rx_data(qs_rf_protocol_t* p_frame)
{ 
    qs_settings_job_t* p_settings_job = qs_settings_job_get();
    uint32_t UID = get_simplified_uid();
    uint32_t RF_UID = (uint32_t)(p_frame->status.uid[0] <<24)|(uint32_t)(p_frame->status.uid[1] <<16)|(uint32_t)(p_frame->status.uid[2] <<8) | (uint32_t)(p_frame->status.uid[3]);
    if(UID == RF_UID || provision_enable){
      switch(p_frame->type) {
      case 2:

         if(p_frame->keys.value != 0) {
              m_rf_keys = p_frame->keys.value;
              if(m_rf_keys == QS_KEYS_PROV){
                m_rf_keys = 0;
                qs_misc_long_beep();
              }
              if(provision_enable)  {
                m_rf_keys = 0; 
              }
              if((p_settings_job->work_mode == QS_ACTION_IDLE_MODE)&&(p_frame->status.work_mode == QS_ACTION_AUTO_SET_MODE))
              {
                m_rf_keys = 0;
              }
              
              
            m_rf_keys_timeout_timer = 5;
          } 
                
          if(m_on_rf_rx_data) {
              m_on_rf_rx_data(p_frame);
          }
          break;
      default:;
      }
   }
    
}

static void on_rx_key_handle(void)
{
    on_keys_20ms_handle();
    if(m_on_ble_rx_data) {
        m_on_ble_rx_data();
    }
    
}


void qs_all_keys_init(qs_key_on_key_evt_handle_s on_key_evt)
{
    
    m_on_key_evt = on_key_evt;
    qs_rf_protocol_rx_mode_event_handle_set(on_rf_rx_data);
    qs_rf_protocol_init(RF_RX_MODE);
    qs_on_keys_20ms_handle_set(on_keys_20ms_handle);
    qs_ble_protocol_on_rx_key_handle_set(on_rx_key_handle);
    qs_ble_protocol_init();
}
void qs_key_on_rf_rx_data_handle_set(qs_key_on_rx_data_handle_s on_rf_rx_data)
{
    m_on_rf_rx_data = on_rf_rx_data;
}
void qs_key_on_ble_rx_data_handle_set(qs_key_on_rf_rx_data_handle_s on_ble_rx_data)
{
    m_on_ble_rx_data = on_ble_rx_data;
}